package com.yien.springstudy.service.impl;

import com.yien.springstudy.service.MemoryExchangeService;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Miaoxiong Fan
 * @since version
 */
@Service
public class MemoryExchangeServiceImpl implements MemoryExchangeService {
    private static int length = 0x8000000; // 128 Mb

    @Override
    public void readAndWriteBuffer() throws IOException {
        // 为了以可读可写的方式打开文件，这里使用RandomAccessFile来创建文件。
        FileChannel fc = new RandomAccessFile("test.dat", "rw").getChannel();
        //注意，文件通道的可读可写要建立在文件流本身可读写的基础之上
        MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);
        //写128M的内容
        for (int i = 0; i < length; i++) {
            out.put((byte) i);
        }
        System.out.println("Finished writing");
        //读取文件中间6个字节内容
        for (int i = length / 2; i < length / 2 + 6; i++) {
            System.out.print((char) out.get(i));
        }
        fc.close();
    }
}
